<?php

namespace App\Api;

use PhalApi\Api;


/**
 * 万国觉醒 登录
 * @author: dogstar <chanzonghuang@gmail.com> 2014-10-04
 */
class RokUesr extends Api
{

    public function getRules()
    {
        return array(
            "login"    => array(
                'username' => array('name' => 'username', 'require' => true, 'desc' => 'username'),
                'password' => array('name' => 'password', 'require' => true, 'desc' => 'password'),
            ),
            "userInfo" => array(
                'username'  => array('name' => 'username', 'desc' => 'username'),
                'password'  => array('name' => 'password', 'desc' => 'password'),
                'tokenName' => array('name' => 'tokenName', 'require' => false, 'desc' => 'password'),
            ),
            "reg"      => array(
                'username' => array('name' => 'username', 'desc' => 'username'),
                'password' => array('name' => 'password', 'desc' => 'password'),
                'power'    => array('name' => 'power', 'desc' => 'power'),
                'note'     => array('name' => 'note', 'desc' => 'note'),
                'creator'  => array('name' => 'creator', 'desc' => 'creator'),
            ),
            "logout"   => array()
        );
    }


    /**
     * 注册接口
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function reg()
    {
        header('Access-Control-Allow-Origin:*');
        $username = $this->username;
        $password = $this->password;
        $power = $this->power;
        $note = $this->note;
        $creator = $this->creator;

        $info = \PhalApi\DI()->notorm->rok_user
            ->where("username", $username)
            ->fetchOne();

        if ($info) {
            \PhalApi\DI()->response->setMsg('success');
            return '已经有帐号';
        }

        \PhalApi\DI()->notorm->rok_user
            ->insert(array(
                'username' => $username,
                'password' => $password,
                'power'    => $power,
                'note'     => $note,
                "creator" => $creator,
                'time'     => date("Y-m-d H:i:s", time()),
                'addtime'  => date("Y-m-d H:i:s", time()),
            ));

        \PhalApi\DI()->response->setMsg('success');
        return '';
    }


    /**
     * logout
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function logout()
    {
        header('Access-Control-Allow-Origin:*');
        \PhalApi\DI()->response->setMsg('success');
        return '';
    }

    /**
     * 通用多条件查询接口
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function userInfo()
    {
        header('Access-Control-Allow-Origin:*');
        \PhalApi\DI()->response->setMsg('success');

        $tokenName = $this->tokenName;

        if (!$tokenName) {
            $tokenName = 'wenfree';
        }

        $userInfo = \PhalApi\DI()->notorm->rok_user
            ->where("username", $tokenName)
            ->fetchOne();

        $res = array(
            "permissions" => array('admin'),
            "username"    => $tokenName,
            "avatar"      => "",
//            "roles"       => array( 'editor' ),
            "roles"       => array($userInfo['power'])
        );

        return $res;
    }

    /**
     * 通用多条件查询接口
     * @desc 多条件查询接口
     * @return array data 被查数据
     * @exception 400 非法请求，参数传递错误
     */
    public function login()
    {
        header('Access-Control-Allow-Origin:*');

        $username = $this->username;
        $password = $this->password;

        $userinfo = \PhalApi\DI()->notorm->rok_user
            ->where(
                array(
                    "username" => $username,
                    "password" => $password,
                )
            )->fetchOne();
        //如果有帐号
        if ($userinfo) {
            $res = array(
                "accessToken" => $username
            );
            \PhalApi\DI()->response->setMsg('success');
            return $res;
        }

    }

}
